home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Graphics 2D / Rubber Bandit / RubberBandit.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-10-06  |  5.2 KB  |  206 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        RubberBandit.c
  3.  
  4.     Contains:    a simple srcXor rubber-banding example.
  5.  
  6.     Written by: DH
  7.  
  8.     Copyright:    Copyright © 1991-1999 by Apple Computer, Inc., All Rights Reserved.
  9.  
  10.                 You may incorporate this Apple sample source code into your program(s) without
  11.                 restriction. This Apple sample source code has been provided "AS IS" and the
  12.                 responsibility for its operation is yours. You are not permitted to redistribute
  13.                 this Apple sample source code as "Apple sample source code" after having made
  14.                 changes. If you're going to re-distribute the source, we require that you make
  15.                 it clear in the source that the code was descended from Apple sample source
  16.                 code, but that you've made changes.
  17.  
  18.     Change History (most recent first):
  19.                 08/2000        JM                Carbonized, non-Carbon code is commented out
  20.                                             for demonstration purposes.
  21.                 7/14/1999    KG                Updated for Metrowerks Codewarror Pro 2.1
  22.                 
  23.  
  24. */
  25. #include "CarbonPrefix.h"
  26. #include <Dialogs.h>
  27. #include <Fonts.h>   
  28. #include <Processes.h>
  29.  
  30. /* constants:             */
  31.  
  32. #define testDLOG    1234
  33. #define OK_button    1
  34.  
  35. /* function prototypes: */
  36.  
  37. extern void HandleDLOG(DialogPtr theDialog);
  38. extern pascal Boolean RubberProc(DialogPtr theDialog, EventRecord *theEvent, short *itemHit);
  39. extern void RubberBandIt(Point anchorPt);
  40. extern Rect *CheckRect(Rect *theRect);
  41. extern void DrawStuff(Rect *rubberRect);
  42. extern void    FanStuff(Rect *theRect);
  43.  
  44.  
  45. void main(void) 
  46. {
  47.     DialogPtr    theDialog = NULL;
  48.  
  49.     /*InitGraf(&qd.thePort);
  50.     InitFonts();
  51.     FlushEvents(everyEvent, 0);
  52.     InitWindows();
  53.     InitMenus();
  54.     TEInit();
  55.     InitDialogs(0L);*/
  56.     InitCursor();
  57.     
  58.     theDialog = GetNewDialog(testDLOG, NULL, (WindowPtr) -1L);
  59.     if (theDialog != NULL) HandleDLOG(theDialog);
  60.         
  61.     ExitToShell();
  62. }
  63.  
  64.  
  65. /*    ================================================
  66.     HandleDLOG calls ModalDialog and rubber-bands
  67.     until the OK button is pressed.
  68.     ================================================    */
  69.  
  70. void HandleDLOG(theDialog)
  71. DialogPtr theDialog;
  72. {
  73.     short        itemHit;
  74.     GrafPtr        oldPort;
  75.     Point        anchorPt;
  76.  
  77.     do
  78.     {
  79.         ModalDialog(NULL, &itemHit);
  80.  
  81.         if (itemHit != OK_button)
  82.         {
  83.             GetPort(&oldPort);            /* Save the port, set it to our dialog, */
  84.             //SetPort(theDialog);
  85.             SetPortWindowPort(GetDialogWindow(theDialog));
  86.             GetMouse(&anchorPt);        /* get the current mouse pos. and        */
  87.             RubberBandIt(anchorPt);        /* rubber-band the image.                */
  88.             SetPort(oldPort);            /* Restore the original port.            */
  89.         }
  90.     }
  91.     while (itemHit != OK_button);
  92.     
  93.     DisposeDialog(theDialog);            /* Throw away the dialog.                */
  94. }
  95.  
  96.  
  97. /*    ================================================
  98.     RubberBandIt follows the mouse and rubber-bands
  99.     a design based on its position.  It retains
  100.     control until the mouse button is released.
  101.     ================================================    */
  102.  
  103. void RubberBandIt(anchorPt)
  104. Point    anchorPt;
  105. {
  106.     Point    curMousePt = {0, 0};
  107.     Rect    rubberRect;
  108.     Pattern gray;
  109.     CGrafPtr    currentPort;
  110.     RgnHandle    rgnHandle = NewRgn();
  111.     
  112.     GetPort(¤tPort);
  113.  
  114.     PenMode(srcXor);                                /* Set pen mode to exclusive or.*/
  115.     PenPat(&gray);
  116.  
  117.     GetMouse(&curMousePt);                            /* Get current mouse pos.        */
  118.  
  119.     rubberRect.top = anchorPt.v;                    /* Draw initial rectangle.        */
  120.     rubberRect.left = anchorPt.h;
  121.     rubberRect.bottom = curMousePt.v;
  122.     rubberRect.right = curMousePt.h;
  123.     DrawStuff(&rubberRect);
  124.  
  125.     while (Button())
  126.     {
  127.         GetMouse(&curMousePt);                        /* Get current mouse pos…        */
  128.  
  129.         if (curMousePt.h != rubberRect.right ||        /* If mouse moved…                */
  130.             curMousePt.v != rubberRect.bottom)
  131.         {
  132.             DrawStuff(&rubberRect);                    /* Erase old…                    */
  133.             rubberRect.bottom = curMousePt.v;
  134.             rubberRect.right = curMousePt.h;
  135.             DrawStuff(&rubberRect);                    /* and draw new.                */
  136.             QDFlushPortBuffer(currentPort, GetPortVisibleRegion(currentPort, rgnHandle));
  137.         }
  138.     }
  139.     
  140.     DrawStuff(&rubberRect);                            /* Erase old at end.            */
  141.     QDFlushPortBuffer(currentPort, GetPortVisibleRegion(currentPort, rgnHandle));
  142.     DisposeRgn(rgnHandle);
  143. }
  144.  
  145.  
  146. /*    ================================================
  147.     DrawStuff draws designs in the rectangle
  148.     passed, using the current pen mode, etc.
  149.     ================================================    */
  150.  
  151. void DrawStuff(rubberRect)
  152. Rect    *rubberRect;
  153. {
  154.     Rect    curRect;
  155.     
  156.     curRect = *rubberRect;
  157.     FrameRect(CheckRect(&curRect));
  158.     FanStuff(CheckRect(&curRect));
  159. }
  160.  
  161.  
  162. /*    ================================================
  163.     FanStuff draws a fanlike thing in the rect
  164.     passed, using the current pen mode, etc.
  165.     ================================================    */
  166.  
  167. void    FanStuff(theRect)
  168. Rect    *theRect;
  169. {
  170.     short    ang;
  171.  
  172.     for (ang = 0; ang < 360; ang += 60)
  173.         PaintArc(theRect, ang, 45);
  174. }
  175.  
  176.  
  177. /*    ================================================
  178.     CheckRect makes sure that the top of the
  179.     rectangle passed is ≤ the bottom and the left is
  180.     ≤ the right.  FrameRect needs things this way.
  181.     ================================================    */
  182.  
  183. Rect *CheckRect(theRect)
  184. Rect    *theRect;
  185. {
  186.     short    temp;
  187.  
  188.     if (theRect->top > theRect->bottom)        /* Need to reverse top and bottom? */
  189.     {
  190.         temp = theRect->top;
  191.         theRect->top = theRect->bottom;
  192.         theRect->bottom = temp;
  193.     }
  194.  
  195.     if (theRect->left > theRect->right)        /* Need to reverse left and right? */
  196.     {
  197.         temp = theRect->left;
  198.         theRect->left = theRect->right;
  199.         theRect->right = temp;
  200.     }
  201.     
  202.     return theRect;                            /* This makes nested calls easier.    */
  203. }
  204.  
  205.  
  206.